<?php
require_once('Extension/Controller/Action.php');

class Admin_PostController extends Extension_Controller_Action
{
    public function indexAction()
    {
        $functionPost = new Core_Model_Function_Post();
        $this->view->posts = $functionPost->findAll();
    }

    public function editAction()
    {
        if ($this->getRequest()->isGet()) {
            $data['id'] = $this->getRequest()->getParam('id');

            $filterResult = $this->getHelper('FaV')->filterPostEditData($data);

            if ($filterResult) {
                $functionPost = new Core_Model_Function_Post();
                $post = $functionPost->findPostById($data['id']);
                if (null != $post) {
                    $this->view->postId      = $post['post_id'];
                    $this->view->postTitle   = $post['post_title'];
                    $this->view->postContent = $post['post_content'];
                }
            } else {
                $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
                $flashMessenger->setNamespace('post')->addMessage($filterResult);
                $this->_redirect('/admin/post/');
            }
        }

        
        //echo $this->getHelper('FaV')->filterAlnum('8129";04jf0fi ~!@- 24kfds');
        //echo $this->getHelper('FaV')->filterAlpha('8129";04jf0fi ~!@- 24kfds');
        //echo $this->getHelper('FaV')->filterBaseName('http://kbs.info/admin/post/edit.php');
        //echo $this->getHelper('FaV')->filterDigits('8129";04jf0fi ~!@- 24kfds');
        //echo $this->getHelper('FaV')->filterDir('G:\WWW\ZendFramework\library/');
        //echo $this->getHelper('FaV')->filterHtmlEntities('"');
        //echo $this->getHelper('FaV')->filterInt('81.29";04jf0fi ~!@- 24kfds');
        //echo $this->getHelper('FaV')->filterRealPath('./../');
        //echo $this->getHelper('FaV')->filterStringToLower('8129";04jf0fi ~!@- 24kfds');
        //echo $this->getHelper('FaV')->filterStringToUpper('8129";04jf0fi ~!@- 24kfds');
        //echo $this->getHelper('FaV')->filterStringTrim("\t\n\r812/.9;04jf0fi ~!@- 24kfds\n\n\t \t");
        //echo $this->getHelper('FaV')->filterStripTags('<a href="www.aramis.com" onclick="do()">click</a><!-- kim -->', 'a', 'href', true);


        $this->view->authorId = Zend_Auth::getInstance()->getIdentity()->user_id;
    }

    public function saveAction()
    {
        if ($this->getRequest()->isPost()) {
            
            $data = $this->getRequest()->getPost();

            $filterResult = $this->getHelper('FaV')->filterPostSaveData($data);

            if (true === $filterResult) {
                $functionPost = new Core_Model_Function_Post();
                if (null != $functionPost->findPostById($data['post_id'])) {
                    $functionPost->save($data, $data['post_id']);
                } else {
                    $data['post_id'] = null;
                    $functionPost->save($data);
                }
                $this->_redirect('/admin/post');
            } else {
                $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
                $flashMessenger->setNamespace('post')->addMessage($filterResult);
                $this->_redirect('/admin/post/');
            }
        }
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isGet()) {
            
            $data['id'] = $this->getRequest()->getParam('id');
            
            $filters = array(
                'id' => 'Int'
            );

            $validators = array(
                'id' => array(Zend_Filter_Input::ALLOW_EMPTY => false)
            );

            $input = new Zend_Filter_Input($filters, $validators);

            $input->setData($data);

            $messages = $input->getMessages();

            $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');

            if ($input->isValid()) {
                $functionPost = new Core_Model_Function_Post();
                $functionPost->delete($data['id']);
            } else {
                $flashMessenger->setNamespace('post')->addMessage(implode(', ', $messages['id']));
            }

            $this->_redirect('/admin/post');
        }
    }



}